<template>
  <div ref="pageBox">
    <SearchBox ref="SearchBox">
      <el-form slot="form" :inline="true" label-width="80px" class="unitWidth" size="small">
        <el-form-item label="用户名">
          <el-input v-model="selname" placeholder="请输入内容"></el-input>
        </el-form-item>
        <el-form-item label="级别">
          <el-input v-model="sellevel" placeholder="请输入"></el-input>
        </el-form-item>
      </el-form>
      <template slot="btns">
        <el-button size="small" type="primary">搜索</el-button>
      </template>
    </SearchBox>
    <LabelContent title="角色管理" ref="LabelContent"> </LabelContent>
    <div class="tableBox">
      <el-table
        stripe
        ref="multipleTable"
        :data="tableData"
        class="center"
        :height="tableHeight + 'px'"
        @selection-change="handleSelectionChange"
        @sort-change="sortChange"
        :default-sort="{ prop: 'value', order: 'descending' }"
      >
        <el-table-column prop="roleName" label="角色名" :sort-method="sortByDate" />
        <el-table-column prop="value" label="级别" :formatter="formatterRow" />
        <el-table-column prop="value" label="创建时间" show-overflow-tooltip />
        <el-table-column label="操作" width="100">
          <template slot-scope="">
            <i class="editBtn iconfont u-youjianbianji" @click="dialogFormVisible = true" />
            <i @click="deleteApi(res)" class="delBtn iconfont u-shanchu" />
          </template>
        </el-table-column>
      </el-table>
      <el-dialog title="角色管理" :visible.sync="dialogFormVisible">
        <el-form style="display: flex; flex-wrap: wrap" :model="form">
          <el-form-item label="用户名" :label-width="formLabelWidth" class="stywid">
            <el-input v-model="setuserinfo.xiuname" placeholder="请输入内容"></el-input>
          </el-form-item>
          <el-form-item label="密码" :label-width="formLabelWidth" class="stywid">
            <el-input v-model="setuserinfo.xiupwd" placeholder="请输入内容"></el-input>
          </el-form-item>
          <el-form-item label="级别" :label-width="formLabelWidth" class="stywid">
            <el-input v-model="setuserinfo.xiulevel" placeholder="请输入内容"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="insert('123')">确 定</el-button>
        </div>
      </el-dialog>
      <el-pagination
        @size-change="paginationChange"
        @current-change="paginationChange"
        :current-page="testValue"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="testValue"
        layout="total, sizes, prev, pager, next, jumper"
        :total="testValue"
      />
    </div>
  </div>
</template>
<script>
export default {
  name: "Factory",
  data () {
    return {
      tableData: [],
      sellevel: "",
      selname: "",
      batchDeleteArr: [],
      tableHeight: 0, //表格高度
      dialogTableVisible: false,
      dialogFormVisible: false,
      formLabelWidth: "120px",
    }
  },
  mounted () {
    //设置表格高度
    this.$nextTick(() => {
      let page = this.$refs.pageBox.offsetHeight || 0 //卡片高度
      let SearchBox = this.$refs.SearchBox
        ? this.$refs.SearchBox.$el.offsetHeight
        : 0 //搜索框高度
      let LabelContent = this.$refs.LabelContent
        ? this.$refs.LabelContent.$el.offsetHeight
        : 0 //内容标题高度
      this.tableHeight = page - (SearchBox + LabelContent + 20 + 30) //padding -20,分页 -30
      this.init()
    })
  },
  methods: {
    async init () {
      let that = this
      that.loading = true
      let result = await that.$http.request({
        url: "/hw/admin/rol/list",
        method: "GET",
      })
      if (result.status == 100) {
        let { data } = result
        that.tableData = data
      }
      console.log(that.tableData)
    },
    batchDelete () {
      let deleteData = this.batchDeleteArr.map((item) => {
        const { server_asset_id } = item
        return { server_asset_id }
      })
      this.deleteApi(deleteData)
    },
    deleteApi (data) {
      const _this = this
      this.$confirm("确定删除？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => { })
    },
    //分页
    paginationChange (val) {
      console.log(val)
    },
    //表格行过滤
    formatterRow (row, column) {
      return "姓名" + row.value
    },
    //多选框选择 返回当前已选行数据(Array)
    handleSelectionChange (val) {
      this.batchDeleteArr = val
      console.log(val)
    },
    //从后台获取数据,重新排序
    sortChange (val) {
      // 详细文档参考： https://element.eleme.cn/2.13/#/zh-CN/component/table
      console.log(val)
    },
    //自定义排序规则 sort-method
    sortByDate (a, b) {
      console.log(a, b)
      if (a < b) return -1
    },
  },
};
</script>

<style lang="scss" scoped>
.stywid {
  width: 40%;
}
</style>
